package com.ruoyi.web.controller.hh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.HhType;
import com.ruoyi.service.IHhTypeService;
import com.ruoyi.utils.SelectHelpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品类型Controller
 * @author chongjin
 * @date 2020-11-26
 */
@Controller
public class HhTypeController extends BaseController {
    private String prefix = "hh/baseInfo";

    @Autowired
    private IHhTypeService hhTypeService;

    @GetMapping("/baseInfo/type")
    public String type() {
        return prefix + "/type_manage";
    }

    /**
     * 查询商品类型列表
     */
    @PostMapping("/baseInfo/type/list")
    @ResponseBody
    public List<HhType> list(HhType hhType) {
        List<HhType> list = hhTypeService.selectHhTypeList(hhType);
        return list;
    }

    /**
     * 查询商品类型
     */
    @PostMapping("/baseInfo/type/getType")
    @ResponseBody
    public AjaxResult getType(HhType hhType) {
        List<HhType> list = hhTypeService.selectHhTypeList(hhType);
        return AjaxResult.success(SelectHelpUtil.getNameType(list));
    }

    /**
     * 新增商品类型
     */
    @GetMapping("/baseInfo/type/add")
    public String add() {
        return prefix + "/type_add";
    }

    /**
     * 新增保存商品类型
     */
    @Log(title = "新增分类", businessType = BusinessType.INSERT)
    @PostMapping("/baseInfo/type/add")
    @ResponseBody
    public AjaxResult addSave(HhType hhType) {
        return toAjax(hhTypeService.insertHhType(hhType));
    }

    /**
     * 修改商品类型
     */
    @GetMapping("/baseInfo/type/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap) {
        HhType hhType = hhTypeService.selectHhTypeById(typeId);
        mmap.put("hhType", hhType);
        return prefix + "/type_edit";
    }

    /**
     * 修改保存商品类型
     */
    @Log(title = "修改分类", businessType = BusinessType.UPDATE)
    @PostMapping("/baseInfo/type/edit")
    @ResponseBody
    public AjaxResult editSave(HhType hhType) {
        return toAjax(hhTypeService.updateHhType(hhType));
    }

    /**
     * 删除商品类型
     */
    @Log(title = "删除分类", businessType = BusinessType.DELETE)
    @PostMapping("/baseInfo/type/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hhTypeService.deleteHhTypeByIds(ids));
    }
}
